package com.baizhi.service.impl;

import com.baizhi.dao.*;
import com.baizhi.entity.*;
import com.baizhi.service.CmfzResourceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * Created by Administrator on 2019/4/20 0020.
 */
@Service
public class CmfzResourceServiceImpl implements CmfzResourceService {
    @Autowired
    private CmfzAdminDao cmfzAdminDao;
    @Autowired
    private CmfzAdminRoleDao cmfzAdminRoleDao;
    @Autowired
    private CmfzRoleDao cmfzRoleDao;
    @Autowired
    private CmfzRoleResourceDao cmfzRoleResourceDao;
    @Autowired
    private CmfzResourceDao cmfzResourceDao;

    private org.slf4j.Logger logger= LoggerFactory.getLogger(CmfzResourceServiceImpl.class);


    @Override
    public List<CmfzResource> queryResource() {
        List<CmfzResource> cmfzResources1 = cmfzResourceDao.selectList(new QueryWrapper<CmfzResource>().isNull("resource_parent_id"));
        for(CmfzResource cmfzResource1:cmfzResources1){
            Integer resourceId1 = cmfzResource1.getResourceId();
            List<CmfzResource> cmfzResources2 = cmfzResourceDao.selectList(new QueryWrapper<CmfzResource>().eq("resource_parent_id", resourceId1));
            cmfzResource1.setCmfzResources(cmfzResources2);
            for(CmfzResource cmfzResource2:cmfzResources2){
                Integer resourceId2 = cmfzResource2.getResourceId();
                List<CmfzResource> cmfzResources3 = cmfzResourceDao.selectList(new QueryWrapper<CmfzResource>().eq("resource_parent_id", resourceId2));
                cmfzResource2.setCmfzResources(cmfzResources3);
            }
        }
        return cmfzResources1;
    }

    @Override
    public Map updateRoleResource(String updateroleName, int[] ids) {
        HashMap map = new HashMap<>();
        try {
            cmfzRoleResourceDao.delete(new QueryWrapper<CmfzRoleResource>().eq("role_name",updateroleName));
            for(Integer i:ids){
                CmfzRoleResource cmfzRoleResource = new CmfzRoleResource();
                cmfzRoleResource.setRoleName(updateroleName);
                cmfzRoleResource.setResourceId(i);
                cmfzRoleResourceDao.insert(cmfzRoleResource);
            }
            map.put("flag",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("flag",false);
        }
        return map;
    }

    @Override
    public List<Integer> showSelected(HttpSession session,String roleName) {
        /*查role_resource表，根据roleName获取资源信息*/
        List<CmfzRoleResource> cmfzRoleResources = cmfzRoleResourceDao.selectList(new QueryWrapper<CmfzRoleResource>().eq("role_name",roleName));
        ArrayList<Integer> resourceIds = new ArrayList<>();
        for(CmfzRoleResource cmfzRoleResource:cmfzRoleResources){
            resourceIds.add(cmfzRoleResource.getResourceId());
        }
        return resourceIds;
    }
}
